#!/bin/bash

# Manual Hostname Setup Guide for 5GC Testbed VMs
# This script provides the commands to manually set hostnames

set -e

# VM Configuration
VM1="192.168.1.206"  # Core Network
VM2="192.168.1.219"  # UPF
VM3="192.168.1.221"  # gNodeB
VM4="192.168.1.205"  # UE

# Target hostnames
HOSTNAME_VM1="vm1-5gc-core"
HOSTNAME_VM2="vm2-5gc-upf"
HOSTNAME_VM3="vm3-5gc-gnb"
HOSTNAME_VM4="vm4-5gc-ue"

# Colors for output
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# Logging function
log() {
    echo -e "${GREEN}[$(date +'%Y-%m-%d %H:%M:%S')] $1${NC}"
}

info() {
    echo -e "${BLUE}$1${NC}"
}

warn() {
    echo -e "${YELLOW}WARNING: $1${NC}"
}

# Function to get current hostname of a VM
get_current_hostname() {
    local vm_ip=$1
    ssh -o ConnectTimeout=10 ubuntu@$vm_ip "hostname" 2>/dev/null || echo "unknown"
}

# Function to show current hostnames
show_current_hostnames() {
    echo
    echo "=========================================="
    echo "        Current VM Hostnames"
    echo "=========================================="
    echo
    
    echo "VM1 (Core Network) - $VM1:"
    echo "  Current: $(get_current_hostname $VM1)"
    echo "  Target:  $HOSTNAME_VM1"
    echo
    
    echo "VM2 (UPF) - $VM2:"
    echo "  Current: $(get_current_hostname $VM2)"
    echo "  Target:  $HOSTNAME_VM2"
    echo
    
    echo "VM3 (gNodeB) - $VM3:"
    echo "  Current: $(get_current_hostname $VM3)"
    echo "  Target:  $HOSTNAME_VM3"
    echo
    
    echo "VM4 (UE) - $VM4:"
    echo "  Current: $(get_current_hostname $VM4)"
    echo "  Target:  $HOSTNAME_VM4"
    echo
}

# Function to generate manual commands
generate_manual_commands() {
    echo
    echo "=========================================="
    echo "        Manual Hostname Setup Commands"
    echo "=========================================="
    echo
    echo "Since sudo requires password authentication, please run these commands manually:"
    echo
    echo "1. SSH to VM1 (Core Network) and set hostname:"
    echo "   ssh vm1"
    echo "   sudo hostnamectl set-hostname $HOSTNAME_VM1"
    echo "   sudo sed -i 's/127.0.1.1.*/127.0.1.1\t$HOSTNAME_VM1/' /etc/hosts"
    echo "   exit"
    echo
    echo "2. SSH to VM2 (UPF) and set hostname:"
    echo "   ssh vm2"
    echo "   sudo hostnamectl set-hostname $HOSTNAME_VM2"
    echo "   sudo sed -i 's/127.0.1.1.*/127.0.1.1\t$HOSTNAME_VM2/' /etc/hosts"
    echo "   exit"
    echo
    echo "3. SSH to VM3 (gNodeB) and set hostname:"
    echo "   ssh vm3"
    echo "   sudo hostnamectl set-hostname $HOSTNAME_VM3"
    echo "   sudo sed -i 's/127.0.1.1.*/127.0.1.1\t$HOSTNAME_VM3/' /etc/hosts"
    echo "   exit"
    echo
    echo "4. SSH to VM4 (UE) and set hostname:"
    echo "   ssh vm4"
    echo "   sudo hostnamectl set-hostname $HOSTNAME_VM4"
    echo "   sudo sed -i 's/127.0.1.1.*/127.0.1.1\t$HOSTNAME_VM4/' /etc/hosts"
    echo "   exit"
    echo
    echo "5. After setting all hostnames, reboot all VMs:"
    echo "   ssh vm1 'sudo reboot'"
    echo "   ssh vm2 'sudo reboot'"
    echo "   ssh vm3 'sudo reboot'"
    echo "   ssh vm4 'sudo reboot'"
    echo
    echo "6. Wait for VMs to come back online, then verify:"
    echo "   ssh vm1 'hostname'"
    echo "   ssh vm2 'hostname'"
    echo "   ssh vm3 'hostname'"
    echo "   ssh vm4 'hostname'"
    echo
}

# Function to create a script file with all commands
create_manual_script() {
    local script_file="manual-hostname-commands.sh"
    
    cat > $script_file << EOF
#!/bin/bash
# Manual Hostname Setup Commands for 5GC Testbed
# Run these commands one by one

echo "Setting hostname for VM1 (Core Network)..."
ssh vm1 << 'VM1_EOF'
sudo hostnamectl set-hostname vm1-5gc-core
sudo sed -i 's/127.0.1.1.*/127.0.1.1\tvm1-5gc-core/' /etc/hosts
echo "VM1 hostname set to: \$(hostname)"
VM1_EOF

echo "Setting hostname for VM2 (UPF)..."
ssh vm2 << 'VM2_EOF'
sudo hostnamectl set-hostname vm2-5gc-upf
sudo sed -i 's/127.0.1.1.*/127.0.1.1\tvm2-5gc-upf/' /etc/hosts
echo "VM2 hostname set to: \$(hostname)"
VM2_EOF

echo "Setting hostname for VM3 (gNodeB)..."
ssh vm3 << 'VM3_EOF'
sudo hostnamectl set-hostname vm3-5gc-gnb
sudo sed -i 's/127.0.1.1.*/127.0.1.1\tvm3-5gc-gnb/' /etc/hosts
echo "VM3 hostname set to: \$(hostname)"
VM3_EOF

echo "Setting hostname for VM4 (UE)..."
ssh vm4 << 'VM4_EOF'
sudo hostnamectl set-hostname vm4-5gc-ue
sudo sed -i 's/127.0.1.1.*/127.0.1.1\tvm4-5gc-ue/' /etc/hosts
echo "VM4 hostname set to: \$(hostname)"
VM4_EOF

echo "All hostnames set. Rebooting VMs..."
ssh vm1 'sudo reboot' &
ssh vm2 'sudo reboot' &
ssh vm3 'sudo reboot' &
ssh vm4 'sudo reboot' &

echo "Waiting 30 seconds for VMs to reboot..."
sleep 30

echo "Verifying hostnames..."
ssh vm1 'hostname'
ssh vm2 'hostname'
ssh vm3 'hostname'
ssh vm4 'hostname'

echo "Hostname setup complete!"
EOF

    chmod +x $script_file
    echo "Created manual script: $script_file"
    echo "You can run: ./$script_file"
    echo
}

# Main function
main() {
    echo
    echo "=========================================="
    echo "      Manual Hostname Setup Guide"
    echo "=========================================="
    echo "Date: $(date)"
    echo "=========================================="
    echo
    
    # Show current hostnames
    show_current_hostnames
    
    # Generate manual commands
    generate_manual_commands
    
    # Create manual script
    create_manual_script
    
    echo "=========================================="
    echo "           Quick Start Option"
    echo "=========================================="
    echo
    echo "Alternatively, you can run the automated script:"
    echo "  ./manual-hostname-commands.sh"
    echo
    echo "This will attempt to set all hostnames automatically."
    echo "You'll need to enter sudo passwords when prompted."
    echo
}

# Handle command line arguments
case "${1:-}" in
    "--help"|"-h")
        echo "Usage: $0 [OPTIONS]"
        echo
        echo "Options:"
        echo "  -h, --help          Show this help message"
        echo "  --show-current      Show current hostnames only"
        echo
        echo "This script provides manual commands to set hostnames"
        echo "since sudo requires password authentication."
        ;;
    "--show-current")
        show_current_hostnames
        ;;
    *)
        main
        ;;
esac 